package com.facemox.sse;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.facemox.bean.ServerMonitor;
import com.facemox.service.StatisticService;
import com.facemox.util.CommandUtil;

@RestController
public class StatisticController {
    
    @Autowired
    private ServerMonitor serverMonitor;
    
    @Autowired
    private StatisticService statisticService;
    
    @RequestMapping(value="/statistic",produces="text/event-stream")
    @Transactional
    public String getFaceCount() throws InterruptedException, IOException {
        
        ArrayList<String> list = new ArrayList<String>();

        StringBuilder sb = null;
        String averageResponseTime = null;
        
        // 查询所有需要监测的服务
        List<ServerMonitor> serverMonitorList = statisticService.getServerMonitor();
        Iterator<ServerMonitor> it = serverMonitorList.iterator();
        
        String osName = System.getProperty("os.name");
        if(osName.contains("Windows")) {
            for(int i=0;i<serverMonitorList.size();i++) {
                ServerMonitor sm = serverMonitorList.get(i);
            
                try {
                    Process exec = Runtime.getRuntime().exec("ping "+ sm.getServerIp());
                    BufferedReader br = new BufferedReader(new InputStreamReader(exec.getInputStream(),"GBk"));
                    String line = null;
                     sb = new StringBuilder();
                    while((line = br.readLine())!=null) {
                        sb.append(line);
                    }
                    String[] arr = sb.toString().split("=");
                    averageResponseTime = arr[arr.length-1];
                    exec.destroy();
                    list.add(averageResponseTime);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        } else {
        for(int i=0;i<serverMonitorList.size();i++) {
            ServerMonitor sm = serverMonitorList.get(i);
            averageResponseTime = CommandUtil.run("/usr/bin/ping -c 4 "+sm.getServerIp());
            list.add(averageResponseTime);
        }
      }
        return "data:"+list+"\n\n";
    }
    
    
    @RequestMapping(value="/face_total",produces="text/event-stream")
    @Transactional
    public String getFaceTotal() {
        
        // 1、统计接口总数
        int faceCount = statisticService.getFaceCount();
        String facecount = String.valueOf(faceCount);
        
        // 2、统计接口执行总数
        int faceExecuteCount = statisticService.getFaceExecuteCount();
        String faceexecutecount = String.valueOf(faceExecuteCount);
        
        // 3、统计链路总数
        int tempFaceLinkCount = statisticService.getFaceLinkCount();
        String faceLinkCount = String.valueOf(tempFaceLinkCount);
        
        // 3、统计链路执行总数
        int tempFaceLinkExecuteCount = statisticService.getFaceLinkExecuteCount();
        String faceLinkExecuteCount = String.valueOf(tempFaceLinkExecuteCount);
        
        
        ArrayList<String> list = new ArrayList<String>();
        list.add(facecount);
        list.add(faceexecutecount);
        list.add(faceLinkCount);
        list.add(faceLinkExecuteCount);
       
        return "data:"+list+"\n\n";
    }
    
    @RequestMapping("add_server_monitor")
    public int addServerMonitor(HttpServletRequest request) {
        String serverName = request.getParameter("server_name");
        String serverIp = request.getParameter("server_ip");
        String remark = request.getParameter("remark");
        serverMonitor.setServerName(serverName);
        serverMonitor.setServerIp(serverIp);
        serverMonitor.setRemark(remark);
        int effectLine = statisticService.addServerMonitor(serverMonitor);
        return effectLine;
    }
    
}
